package cn.iocoder.yudao.module.resource.controller.admin.resource;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.resource.controller.admin.resource.vo.*;
import cn.iocoder.yudao.module.resource.dal.dataobject.resource.ResourceDO;
import cn.iocoder.yudao.module.resource.convert.resource.ResourceConvert;
import cn.iocoder.yudao.module.resource.service.resource.ResourceService;

@Tag(name = "管理后台 - 资源管理 - 视频 音频 字幕等")
@RestController
@RequestMapping("/qhsx/resource")
@Validated
public class ResourceController {

    @Resource
    private ResourceService resourceService;

    @PostMapping("/create")
    @Operation(summary = "创建资源管理 - 视频 音频 字幕等")
    @PreAuthorize("@ss.hasPermission('qhsx:resource:create')")
    public CommonResult<Long> createResource(@Valid @RequestBody ResourceCreateReqVO createReqVO) {
        return success(resourceService.createResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源管理 - 视频 音频 字幕等")
    @PreAuthorize("@ss.hasPermission('qhsx:resource:update')")
    public CommonResult<Boolean> updateResource(@Valid @RequestBody ResourceUpdateReqVO updateReqVO) {
        resourceService.updateResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源管理 - 视频 音频 字幕等")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qhsx:resource:delete')")
    public CommonResult<Boolean> deleteResource(@RequestParam("id") Long id) {
        resourceService.deleteResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源管理 - 视频 音频 字幕等")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qhsx:resource:query')")
    public CommonResult<ResourceRespVO> getResource(@RequestParam("id") Long id) {
        ResourceDO resource = resourceService.getResource(id);
        return success(ResourceConvert.INSTANCE.convert(resource));
    }

    @GetMapping("/list")
    @Operation(summary = "获得资源管理 - 视频 音频 字幕等列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qhsx:resource:query')")
    public CommonResult<List<ResourceRespVO>> getResourceList(@RequestParam("ids") Collection<Long> ids) {
        List<ResourceDO> list = resourceService.getResourceList(ids);
        return success(ResourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源管理 - 视频 音频 字幕等分页")
    @PreAuthorize("@ss.hasPermission('qhsx:resource:query')")
    public CommonResult<PageResult<ResourceRespVO>> getResourcePage(@Valid ResourcePageReqVO pageVO) {
        PageResult<ResourceDO> pageResult = resourceService.getResourcePage(pageVO);
        return success(ResourceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源管理 - 视频 音频 字幕等 Excel")
    @PreAuthorize("@ss.hasPermission('qhsx:resource:export')")
    @OperateLog(type = EXPORT)
    public void exportResourceExcel(@Valid ResourceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ResourceDO> list = resourceService.getResourceList(exportReqVO);
        // 导出 Excel
        List<ResourceExcelVO> datas = ResourceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "资源管理 - 视频 音频 字幕等.xls", "数据", ResourceExcelVO.class, datas);
    }

}
